/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrt;

import java.io.File;

import com.atjava.jcrt.core.JcrtSessionFactoryImpl;
import com.atjava.jcrt.core.SystemProperty;
import com.atjava.jcrt.core.exception.CoreException;
import com.atjava.jcrt.core.transaction.XmlJcrtInfo;
import com.atjava.jcrt.repository.Provider;


/**
 * JcrtManagerFactory
 * 
 * @author biggie
 * @version $Revision: 1.2 $ $Date: 2008/07/23 07:18:38 $
 */
public abstract class JcrtSessionFactory {

	private boolean startServiceFlag = false;
	private static String configuration_file = null;
	/** jcrtManager factory instace */
	private static JcrtSessionFactory factory = null;
	/**
	 * Get the Instance for this JcrtManager factory
	 * @return
	 */
	public static synchronized JcrtSessionFactory getInstance() throws CoreException {
		if(factory == null){
			factory = new JcrtSessionFactoryImpl();
		}
		if(configuration_file == null){
			configuration_file = SystemProperty.SYSTEM_REALPATH + File.separator+ "WEB-INF" + File.separator+ SystemProperty.DEFAULT_JCRT_CFG ;    	
		}
		if(!factory.startServiceFlag){
			//Start Jcrt Services
			factory.startServices();
		}
		return factory;
	}
	/**
	 * Create JcrtSession
	 * 
	 * @return JcrtSession
	 * @throws CoreException
	 */
	public abstract JcrtSession openJcrtSession() throws CoreException;
	/**
	 * Create JcrtSession
	 * 
	 * @param workspace
	 * @return JcrtSession
	 * @throws CoreException
	 */
	public abstract JcrtSession openJcrtSession(String workspace) throws CoreException;
	/**
	 * Get XmlJcrtInfo
	 * @return
	 * @throws CoreException
	 */
	public abstract XmlJcrtInfo getXmlJcrtInfo() throws  CoreException;
	/**
	 * Start Services
	 * @throws CoreException
	 */
	public abstract void startServices() throws CoreException;

	/**
	 * Get Provider
	 * @throws CoreException
	 */
	public abstract Provider getProvider() throws CoreException;

	/**
	 * Set Start Service Flag
	 * @param flag
	 */
	protected void setStartServiceFlag(boolean flag){
		this.startServiceFlag = flag;
	}
	
	public boolean getStartServiceFlag(){
		return this.startServiceFlag;
	}
	
	public String getConfigurationfile() {
		return configuration_file;
	}
	/**
	 * Instantiate a new JcrtSessionFactory, using the properties and mappings in this configurationfile.
	 * 
	 * @param configurationfile
	 * @return
	 * @throws CoreException
	 */
	public static synchronized JcrtSessionFactory buildSessionFactory(String configurationfile) throws CoreException{
		if(factory == null){
			factory = new JcrtSessionFactoryImpl();
		}
		if(configurationfile == null){
			throw new CoreException("JcrtSessionFactory:Configuration_file is null !");
		}
		configuration_file = configurationfile;
		//Start Services
		factory.startServices();
		return factory;
	}


}